package com.avob.openadr.server.common.vtn.models.vengroup;

import java.io.Serializable;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.PreRemove;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

import com.avob.openadr.server.common.vtn.models.ven.Ven;

/**
 * VenGroup persistent object - database representation
 * 
 * @author bertrand
 *
 */
@Entity
@Table(name = "vengroup")
public class VenGroup implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -5954213129857481718L;

    /**
     * Autogenerated unique id
     */
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @NotNull
    @Column(unique = true)
    private String name;
    
    private String description;

    @ManyToMany(mappedBy = "venGroups")
    private Set<Ven> vens;

    public VenGroup() {
    }

    public VenGroup(String name) {
        this.name = name;
    }

    @PreRemove
    public void removeVenGroupFromGroup() {
        if (vens != null) {
            for (Ven ven : vens) {
                ven.removeGroup(this);
            }
        }
    }

    public Set<Ven> getVens() {
        return vens;
    }

    public void setVens(Set<Ven> vens) {
        this.vens = vens;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    /**
     * @return the name
     */

    public String getName() {
        return name;
    }

    /**
     * @param name
     *            the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}
}
